// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================
package tc.common.utility;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.JPanel;

public class GrView extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public  TcDisplayTransf transf;
	public LinkedList<GrObject> objects = new LinkedList<GrObject>();
	private Listener listener;
	ListenerEx listex = new ListenerEx(this);
	
	public enum Type {UNDEF, GRAPH, DETAIL};
	
	public Type type = Type.UNDEF;
	
	public GrView() {
		setBackground(Color.WHITE);
	   transf = new TcDisplayTransf(new Point2D.Double(0,0), 0.001);
		
	   listener = new Listener(this);
	   addMouseListener((MouseListener) listener); 
	   addMouseMotionListener((MouseMotionListener) listener); 
	   addComponentListener((ComponentListener) listex);
	}
	
	public Iterator<GrObject> iterator() {
		return objects.iterator();
	}
		
	public void grvAdd(GrObject object) {
		objects.add(object);
	}
	
	public void grvDisplay(Graphics g) {
		Iterator<GrObject> it = objects.iterator();
		while ( it.hasNext() )
		{	GrObject oi = it.next();
			if (oi.visible == false) continue;
			oi.display(g, this);
		}
	}
	
	public void paint(Graphics g)
	{	g.clearRect(0, 0, getWidth(), getHeight());
		grvDisplay(g);
	}
	
	public void refreshModel() {repaint(); };
	
	class Listener implements  MouseListener, MouseMotionListener {
		protected GrView view;

		private boolean pointEntered;
		private Point point;
		
		public Listener(GrView view) {
		    this.view = view;
		}
		
		public void mousePressed(MouseEvent e) {
			if (e.getButton() == MouseEvent.BUTTON1) {
				TcCmd.activeCommand.enterPoint(e.getPoint(), view);
			}
			if (e.getButton() == MouseEvent.BUTTON2) {
			}
			if (e.getButton() == MouseEvent.BUTTON3) {
				TcCmd.activeCommand.enterReset(e.getPoint(), view);
			}
		} 
		public void mouseReleased(MouseEvent e) {}    
		public void mouseDragged(MouseEvent e) {}
		public void mouseClicked(MouseEvent e) {}
		public void mouseEntered(MouseEvent e) {}  
		public void mouseExited(MouseEvent e) {
			Dynbuff.dynamicBufferClean();
		}
		public void mouseMoved(MouseEvent e) {
			TcCmd.activeCommand.enterMove(e.getPoint(), view);
		}     
	}
	
	class ListenerEx implements  ComponentListener {
		protected GrView view;
		
		public ListenerEx(GrView grView) {
		    this.view = grView;
		}

		public void componentResized(ComponentEvent e) {
			view.transf.width  = 0.00025*(double)view.getWidth();
			view.transf.height = 0.00025*(double)view.getHeight();
			refreshModel();
		}
		public void componentHidden(ComponentEvent e) {}	
		public void componentMoved(ComponentEvent e) {} 
		public void componentShown(ComponentEvent e) {} 
	}
}
